package com.ujs.eduservice.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ujs.commonutils.constant.R;
import com.ujs.eduservice.entity.EduTeacher;
import com.ujs.eduservice.entity.vo.TeacherQuery;
import com.ujs.eduservice.service.EduTeacherService;
import com.ujs.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-07-28
 */
@RestController
@Api(tags = "教师管理api")
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    // 查询讲师表数据
    @GetMapping("/findAll")
    @ApiOperation("查找所有教师")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除指定教师")
    public R deleteTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) return R.ok();
        else return R.error();
    }


    @PostMapping("/pageTeacher/{current}/{size}")
    @ApiOperation("条件查询带分页查询")
    public R pageTeacher(@PathVariable("current") Long current,
                         @PathVariable("size") Long size,
                         @RequestBody(required = false) TeacherQuery teacherQuery) {
        IPage<EduTeacher> page = new Page<>(current, size);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            queryWrapper.like("name", teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) {
            queryWrapper.like("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            queryWrapper.le("gmt_create", teacherQuery.getEnd());
        }
        queryWrapper.orderByDesc("gmt_modified");
        teacherService.page(page, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());

        return R.ok().data(map);
    }


    @PostMapping("/addTeacher")
    @ApiOperation("添加讲师")
    public R addTeacher(@RequestBody EduTeacher teacher) {
        try {
            teacherService.save(teacher);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "保存讲师失败");
        }
    }


    @GetMapping("/teacher/{id}")
    @ApiOperation("根据id获取讲师")
    public R getTeacherById(@PathVariable("id") String id) {
        EduTeacher teacher = teacherService.getById(id);
        // try {
        //     int i = 10 / 0;
        // } catch (Exception e) {
        //     throw new GuliException(20001, "执行了自定义异常处理！！");
        // }
        return R.ok().data("teacher", teacher);
    }

    @PostMapping("/update")
    @ApiOperation("修改讲师")
    public R updateTeacher(@RequestBody EduTeacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) return R.ok();
        else return R.error();
    }
}




